<?php
require_once 'EZend/Controller/WebBaseController.php';

class AdminBaseController extends WebBaseController {
	/**
	 * @var CurrentAdminUser
	 */
	protected $currentAdminUser = null;
	
	public function init() {
		parent::init();
		$this->view->headTitle()->prepend('管理后台');
		
		$moduleName = $this->getRequest()->getModuleName();
		$controllerName = $this->getRequest()->getControllerName();
		
		Zend_Controller_Front::getInstance()->getPlugin('Zend_Controller_Plugin_ErrorHandler')->setErrorHandlerModule($moduleName);
		
		$existSessionId = $this->getRequest()->getCookie(SiteConfig::ADMIN_COOKIE_NAME);
		$existSession = $this->getAdminSessionService()->getSession($existSessionId);
		if ($existSession) {
			$adminUser = $this->getAdminUserService()->get($existSession['user_id']);
			if (!$adminUser) {
				$this->getAdminSessionService()->deleteSession($existSessionId);
				return $this->_redirect($this->view->url(array('module' => 'admin', 'controller' => 'login'), 'default', true));
			}
			$this->getCurrentAdminUser()->setUser($adminUser + array('session' => $existSession));
			
			setcookie(SiteConfig::ADMIN_COOKIE_NAME, $existSessionId, time() + SiteConfig::ADMIN_COOKIE_EXPIRE, '/', null, false, true);
		} elseif (!($moduleName == 'admin' && $controllerName == 'login')) {
			return $this->_redirect($this->getInnerUrl(array(
				'module' => 'admin', 
				'controller' => 'login', 
				'from' => urlencode($this->getInnerUrl(array('module' => $moduleName, 'controller' => $controllerName)))
			)));
		}
		
		$this->view->assign('currentAdminUser', $this->getCurrentAdminUser());
	}
	
	/**
	 * @return CurrentAdminUser
	 */
	protected function getCurrentAdminUser() {
		if (!$this->currentAdminUser) {
			$this->currentAdminUser = CurrentAdminUser::getInstance();
		}
		return $this->currentAdminUser;
	}
	
	protected function getAdminUserService() {
		require_once 'Models/Admin/Service/AdminServiceFactory.php';
		return AdminServiceFactory::getInstance()->createAdminUserService();
	}
	
	protected function getAdminSessionService() {
		require_once 'Models/Admin/Service/AdminServiceFactory.php';
		return AdminServiceFactory::getInstance()->createAdminSessionService();
	}

}

class CurrentAdminUser {
	private static $instance;
	private $adminUser = null;
	private $userPermissions = null;
	private $permissionKey = '';
	
	private function __construct() {
	}
	
	/**
	 * @return CurrentAdminUser
	 */
	public static function getInstance() {
		if (!self::$instance) self::$instance = new self();
		return self::$instance;
	}
	
	public function setUser($adminUser) {
		$this->adminUser = $adminUser;
		return $this;
	}
	
	public function getId() {
		return $this->getUserInfo('user_id');
	}
	
	public function getName() {
		return $this->getUserInfo('user_name');
	}
	
	public function getUserInfo($key = null, $default = null) {
		if (null === $key) return $this->adminUser;
		return isset($this->adminUser[$key]) ? $this->adminUser[$key] : $default;
	}
	
	public function isGod() {
		return (bool) $this->getUserInfo('is_god');
	}
	
	public function getPermissionList() {
		return include(dirname(__FILE__) . '/../../../configs/admin/menu.php');
	}
	
	
	protected function getAdminUserService() {
		require_once 'Models/Admin/Service/AdminServiceFactory.php';
		return AdminServiceFactory::getInstance()->createAdminUserService();
	}
}

